package com.sfx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfx.domain.entity.LoginUser;
import com.sfx.domain.entity.User;
import com.sfx.enums.AppHttpCodeEnum;
import com.sfx.exception.SystemException;
import com.sfx.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    //如果重写会默认使用这个UserDetailsService的实现类,帮我们去查询数据库进行校验用户
    @Autowired
    private UserMapper userMapper;
    
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserName,userName);
        User user = userMapper.selectOne(queryWrapper);
        if(user == null) {
            throw new SystemException(AppHttpCodeEnum.LOGIN_ERROR);
        }
        return new LoginUser(user);
    }
}